home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Moscow ML 1.31 / source code / mosml / src / mosmllib / Substring.sig < prev    next >
Encoding:
Text File  |  1996-07-03  |  8.8 KB  |  205 lines  |  [TEXT/R*ch]

  1. (* Substring -- SML Standard Library *)
  2.  
  3. type substring
  4.  
  5. val substring : string * int * int -> substring
  6. val extract   : string * int * int option -> substring
  7. val all       : string -> substring
  8. val string    : substring -> string
  9. val base      : substring -> (string * int * int)
  10.  
  11. val isEmpty   : substring -> bool
  12. val getc      : substring -> (char * substring) option
  13. val first     : substring -> char option
  14. val triml     : int -> substring -> substring
  15. val trimr     : int -> substring -> substring
  16. val sub       : substring * int -> char
  17. val size      : substring -> int
  18. val slice     : substring * int * int option -> substring
  19. val concat    : substring list -> string
  20. val explode   : substring -> char list
  21. val compare   : substring * substring -> General.ordering
  22. val collate   : (char * char -> ordering) -> substring * substring -> ordering
  23.  
  24. val dropl     : (char -> bool) -> substring -> substring
  25. val dropr     : (char -> bool) -> substring -> substring
  26. val takel     : (char -> bool) -> substring -> substring
  27. val taker     : (char -> bool) -> substring -> substring
  28. val splitl    : (char -> bool) -> substring -> substring * substring
  29. val splitr    : (char -> bool) -> substring -> substring * substring
  30.  
  31. val splitAt   : substring * int -> substring * substring
  32.  
  33. val position  : string -> substring -> substring
  34.  
  35. val translate : (char -> string) -> substring -> string
  36.  
  37. val tokens    : (char -> bool) -> substring -> substring list
  38. val fields    : (char -> bool) -> substring -> substring list
  39.  
  40. val foldl     : (char * 'a -> 'a) -> 'a -> substring -> 'a
  41. val foldr     : (char * 'a -> 'a) -> 'a -> substring -> 'a
  42. val app       : (char -> unit) -> substring -> unit
  43.  
  44. val scanString :
  45.     ({getc : int -> (char * int) option} -> int -> ('b * int) option)
  46.     -> substring -> 'b option
  47.  
  48.  
  49. (* A substring is an abstract representation of a piece of a string.
  50.    The type [substring] is the abstract type of substrings of a basestring.
  51.    A substring (s,i,n) is valid if 0 <= i <= i+n <= size s, 
  52.                   or equivalently, 0 <= i and 0 <= n and i+n <= size s.  
  53.    A valid substring (s, i, n) represents the string s[i...i+n-1].  
  54.    Invariant in the implementation: Any value of type substring is valid.
  55.  
  56.    [substring(s, i, n)] creates the substring (s, i, n), consisting of
  57.    the substring of s with length n starting at i.  Raises Subscript
  58.    if i<0 or n<0 or i+n > size s.  Equivalent to extract(s, i, SOME n).
  59.  
  60.    [extract(s, i, NONE)] creates the substring (s, i, size s-i)
  61.    consisting of the tail of s starting at i.  
  62.    Raises Subscript if i<0 or i > size s.
  63.  
  64.    [extract(s, i, SOME n)] creates the substring (s, i, n),
  65.    consisting of the substring of s with length n starting at i.
  66.    Raises Subscript if i<0 or n<0 or i+n > size s.
  67.  
  68.    [all s] is the substring (s, 0, size s).
  69.  
  70.    [string sus] is the string s[i..i+n-1] represented by sus = (s, i, n).
  71.  
  72.    [base sus] is the concrete triple (s, i, n), where sus = (s, i, n).
  73.  
  74.    [isEmpty (s, i, n)] true if the substring is empty (that is, n = 0).
  75.  
  76.    [getc sus] returns SOME(c, rst) where c is the first character and
  77.    rst the remainder of sus, if sus is non-empty; otherwise returns
  78.    NONE.  
  79.  
  80.    [first sus] returns SOME c where c is the first character in sus,
  81.    if sus is non-empty; otherwise returns NONE.
  82.  
  83.    [triml k sus] returns sus less its leftmost k characters; or the empty 
  84.    string at the end of sus if it has less than k characters.
  85.  
  86.    [trimr k sus] returns sus less its rightmost k characters; or the empty 
  87.    string at the beginning of sus if it has less than k characters.
  88.  
  89.    [sub (sus, k)] returns the k'th character of the substring; that is,
  90.    s(i+k) where sus = (s, i, n).  Raises Subscript if k<0 or k>=n.
  91.  
  92.    [size (s, i, n)] returns the size of the substring, that is, n.
  93.  
  94.    [slice (sus, i', n')] returns the substring (s, i+i', n'), where
  95.    sus = (s, i, n).  Raises Subscript if i' < 0 or n' < 0 or i'+n' >= n.
  96.  
  97.    [concat suss] returns a string consisting of the concatenation of
  98.    the substrings.  Equivalent to String.concat (List.map string suss).
  99.  
  100.    [explode sus] returns the list of characters of sus, that is,
  101.        [s(i), s(i+1), ..., s(i+n-1)]
  102.    where sus = (s, i, n).  Equivalent to String.explode(string ss).
  103.  
  104.    [compare (sus1, sus2)] performs lexicographic comparison, using the
  105.    standard ordering Char.compare on the characters.  Returns LESS,
  106.    EQUAL, or GREATER, according as sus1 is less than, equal to, or
  107.    greater than sus2.  Equivalent to, but more efficient than,
  108.        String.compare(string sus1, string sus2).
  109.  
  110.    [collate cmp (sus1, sus2)] performs lexicographic comparison, using the 
  111.    given ordering cmp on characters.  Equivalent to, but more efficient 
  112.    than, String.collate cmp (string sus1, string sus2).
  113.  
  114.    [dropl p sus] drops the longest prefix (left substring) of sus all
  115.    of whose characters satisfy predicate p.  If all characters do, it
  116.    returns the empty substring (s, i+n, 0) where sus = (s, i, n).
  117.  
  118.    [dropr p sus] drops the longest suffix (right substring) of sus all
  119.    of whose characters satisfy predicate p.  If all characters do, it
  120.    returns the empty substring (s, i, 0) where sus = (s, i, n).
  121.  
  122.    [takel p sus] returns the longest prefix (left substring) of sus
  123.    all of whose characters satisfy predicate p.  That is, if the
  124.    left-most character does not satisfy p, returns the empty (s, i, 0)
  125.    where sus = (s, i, n).
  126.  
  127.    [taker p sus] returns the longest suffix (right substring) of sus
  128.    all of whose characters satisfy predicate p.  That is, if the
  129.    right-most character satisfies p, returns the empty (s, i+n, 0)
  130.    where sus = (s, i, n).
  131.  
  132.    Let p be a predicate and xxxxfyyyyfzzzz a string where all
  133.    characters in xxxx and zzzz satisfy p, and f a is character
  134.    not satisfying p.  Then
  135.  
  136.                 sus = xxxxfyyyyfzzzz         sus = xxxxzzzz
  137.         ------------------------------------------------------
  138.         dropl p sus =     fyyyyfzzzz               
  139.         dropr p sus = xxxxfyyyyf       
  140.         takel p sus = xxxx                         xxxxzzzz
  141.         taker p sus =           zzzz               xxxxzzzz
  142.  
  143.    It also holds that 
  144.         concat[takel p sus, dropl p sus] = string sus
  145.         concat[dropr p sus, taker p sus] = string sus 
  146.  
  147.    [splitl p sus] splits sus into a pair (sus1, sus2) of substrings
  148.    where sus1 is the longest prefix (left substring) all of whose
  149.    characters satisfy p, and sus2 is the rest.  That is, sus2 begins
  150.    with the leftmost character not satisfying p.  Disregarding
  151.    sideeffects, we have: 
  152.     splitl p sus = (takel p sus, dropl p sus).
  153.  
  154.    [splitr p sus] splits sus into a pair (sus1, sus2) of substrings
  155.    where sus2 is the longest suffix (right substring) all of whose
  156.    characters satisfy p, and sus1 is the rest.  That is, sus1 ends
  157.    with the rightmost character not satisfying p.  Disregarding
  158.    sideeffects, we have:
  159.     splitr p sus = (dropr p sus, taker p sus)
  160.  
  161.    [splitAt (sus, k)] returns the pair (sus1, sus2) of substrings,
  162.    where sus1 contains the first k characters of sus, and sus2
  163.    contains the rest.  Raises Subscript if k < 0 or k > size sus.
  164.  
  165.    [position s (s',i,n)] returns the longest suffix substring of 
  166.    (s', i, n) which has s as a prefix, if any.  More precisely, 
  167.    let m = size s.  If there is a least index k in i..i+n-m for
  168.    which s = s'[k..k+m-1], then the result is (s, k, n-(k-i)); 
  169.    otherwise the result is (s, i+n, 0).
  170.  
  171.    [translate f sus] applies f to every character of sus, from left to
  172.    right, and returns the concatenation of the results.  Equivalent to
  173.    String.concat(List.map f (explode sus)).
  174.  
  175.    [tokens p sus] returns the list of tokens in sus, from left to right, 
  176.    where a token is a non-empty maximal substring of sus not containing 
  177.    any delimiter, and a delimiter is a character satisfying p.
  178.  
  179.    [fields p sus] returns the list of fields in sus, from left to right, 
  180.    where a field is a (possibly empty) maximal substring of sus not 
  181.    containing any delimiter, and a delimiter is a character satisfying p.
  182.  
  183.    Two tokens may be separated by more than one delimiter, whereas two
  184.    fields are separated by exactly one delimiter.  If the only delimiter 
  185.    is the character #"|", then
  186.        "abc||def" contains two tokens:   "abc" and "def"
  187.        "abc||def" contains three fields: "abc" and "" and "def"
  188.  
  189.    [foldl f e sus] folds f over sus from left to right.  That is, 
  190.    evaluates f(s[i+n-1], f( ... f(s[i+1], f(s[i] % e)) ...)) 
  191.    tail-recursively, where sus = (s, i, n).  
  192.    Equivalent to List.foldl f e (explode sus).
  193.  
  194.    [foldr f e sus] folds f over sus from right to left.  That is, 
  195.    evaluates f(s[i], f(s[i+1], f(... f(s[i+n-1] % e) ...)))
  196.    tail-recursively, where sus = (s, i, n).
  197.    Equivalent to List.foldr f e (explode sus).
  198.  
  199.    [app f sus] applies f to all characters of sus, from left to right.
  200.    Equivalent to List.app f (explode sus).
  201.  
  202.    [scanString scan sus] turns the substring sus into a character source 
  203.    and applies `scan' to that source.
  204. *)
  205.